package scau.gsmg.misp.action;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import scau.gsmg.misp.service.IReturnGoodsService;

import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @Title 退货
 * @Group gaoshenmegui
 * @author ZCL
 * @date 2015年5月9日
 */
@Controller
@Scope("prototype")
@Namespace(value = "/")
@ParentPackage("json-default")
@Results({
		@Result(name = "success", type = "json", params = { "root", "jsonObj" }),
		@Result(name = "error", location = "/error.jsp") })
public class ReturnGoodsAction extends ActionSupport  {

	private static final long serialVersionUID = 762580404762395199L;
	private static final Logger logger = Logger.getLogger(ReturnGoodsAction.class);
	private String paymentId;
	private JSONObject jsonObj;
	private String rowObj;
	private int userId;
	private String returnId;
	private double total;
	private int amount;
	private int integral;
	private int customerId;
	protected IReturnGoodsService returnGoodsService;
	
	/**
	 * @Description 通过小票流水号搜索支付单，返回支付单的商品
	 */
	@Action(value = "searchPayment")
	public String searchPayment() throws Exception {
		JSONObject data = new JSONObject();
		List list = returnGoodsService.searchPayment(paymentId);
		List customerInfo = returnGoodsService.gainSaleOrder(paymentId);
		if(list.size()>0){
			data.put("rows", list);
			data.put("customerInfo", customerInfo);
			data.put("success", true);
		}else{
			data.put("success", false);
			data.put("message", "支付单不存在！");
		}
		jsonObj = data;
		return "success";
	}
	
	/**
	 * @Description 获取退货单号
	 */
	@Action(value = "gainReturnId")
	public String gainReturnId() throws Exception {
		String returnId = returnGoodsService.gainReturnId();
		JSONObject data = new JSONObject();
		data.put("success", true);
		data.put("message", returnId);
		jsonObj = data;
		return "success";
	}
	
	/**
	 * @Description 退货
	 */
	@Action(value = "returnGoods")
	public String returnGoods() throws Exception {
		JSONObject data = new JSONObject();
		if(returnGoodsService.returnGoods(rowObj, paymentId, userId, returnId, total, amount, integral, customerId)){
			data.put("success", true);
			data.put("message", "已成功退货！");
		}else{
			data.put("success", false);
			data.put("message", "失败！");
		}
		jsonObj = data;
		return "success";
	}
	
	public IReturnGoodsService getReturnGoodsService() {
		return returnGoodsService;
	}
	@Resource
	public void setReturnGoodsService(IReturnGoodsService returnGoodsService) {
		this.returnGoodsService = returnGoodsService;
	}

	public String getPaymentId() {
		return paymentId;
	}

	public void setPaymentId(String paymentId) {
		this.paymentId = paymentId;
	}

	public JSONObject getJsonObj() {
		return jsonObj;
	}

	public void setJsonObj(JSONObject jsonObj) {
		this.jsonObj = jsonObj;
	}
	public int getUserId() {
		return userId;
	}
	public void setUserId(int userId) {
		this.userId = userId;
	}
	public String getReturnId() {
		return returnId;
	}
	public void setReturnId(String returnId) {
		this.returnId = returnId;
	}
	public double getTotal() {
		return total;
	}
	public void setTotal(double total) {
		this.total = total;
	}
	public int getAmount() {
		return amount;
	}
	public void setAmount(int amount) {
		this.amount = amount;
	}
	public int getIntegral() {
		return integral;
	}
	public void setIntegral(int integral) {
		this.integral = integral;
	}
	public String getRowObj() {
		return rowObj;
	}
	public void setRowObj(String rowObj) {
		this.rowObj = rowObj;
	}

	public int getCustomerId() {
		return customerId;
	}

	public void setCustomerId(int customerId) {
		this.customerId = customerId;
	}
	
}
